package concurrency.chapter3;

import java.util.concurrent.TimeUnit;

/**
 * @author honway.liu
 * @date 2019-06-04 23:33
 * @email gm100861@gmail.com
 * @blog http://linuxsogood.org
 */
public class ThreadCloseDemo2 {

    static class DemoThread extends Thread {

        @Override
        public void run() {
            while (true) {
                try {
                    TimeUnit.SECONDS.sleep(2);
                    System.out.println("demo thread is run...");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println("收到线程被打断的信息,停止执行该线程");
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e1) {
                        e1.printStackTrace();
                    }
                    break;
                }
            }
            System.out.println("整个run方法将停止执行.");
        }
    }

    public static void main(String[] args) throws InterruptedException {
        DemoThread demoThread = new DemoThread();
        demoThread.start();
        TimeUnit.SECONDS.sleep(10);
        demoThread.interrupt();
        System.out.println("主线程执行完成.");
    }
}
